package com.yanqu.road.server.manager.activity.channelmusicroomrank;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.channelmusicroomrank.ChannelMusicRoomRankConfig;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.bussiness.servercenter.ChannelActivityBussiness;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.*;

public class CrossChannelMusicRoomRankConfigMgr extends TempCrossMgr {
    private static Map<Integer, ChannelMusicRoomRankConfig> configMap = new HashMap<>();

    @Override
    public boolean removeExpireData() throws Exception {
        return false;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        reloadActivityConfig();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static synchronized void reloadActivityConfig() {
        getLogger().info("reload CrossChannelMusicRoomRankConfigMgr start");

        // 先检查有没有数据库连接配置
        String dbParam = ChannelConfig.CHANNEL_CENTER_DB_PARAM;
        if ("0".equals(dbParam)) {
            getLogger().error("[ ChannelConfig.CHANNEL_CENTER_DB_PARAM ] is zero!");
            return;
        }

        Map<Integer, ActivityInfo> activityInfoMap = ChannelActivityBussiness.getOpeningActivityInfoMapByType(dbParam, ChannelConfig.CHANNEL_ACTIVITY_TIME_ZONE,eActivityType.ChannelMusicRoomRank.getValue(),1);
        for (ActivityInfo item : activityInfoMap.values()) {
            reloadConfig(item);
            getLogger().info("reload CrossChannelMusicRoomRankConfigMgr config activityId{} end", item.getActivityId());
        }
        getLogger().info("reload CrossChannelMusicRoomRankConfigMgr end");
    }

    public static synchronized ChannelMusicRoomRankConfig reloadConfig(ActivityInfo activityInfo) {
        String dbParam = ChannelConfig.CHANNEL_CENTER_DB_PARAM;

        Map<String, ActivityConfig> activityConfigMap = ChannelActivityBussiness.getActivityConfigMap(dbParam,Collections.singletonList(activityInfo.getActivityId())).get(activityInfo.getActivityId());
        ChannelMusicRoomRankConfig config = new ChannelMusicRoomRankConfig(activityInfo,activityConfigMap);
        configMap.put(config.getActivityInfo().getActivityId(), config);

        return config;
    }

    public static ChannelMusicRoomRankConfig getConfig(int activityId) {
        return configMap.get(activityId);
    }

    public static void removeConfig(int activityId) {
        configMap.remove(activityId);
    }
}
